<?php
/**
 * SBOrderProcessor.php
 * description:
 * Created by www.
 * User: SCF
 * Date: 2018/9/8 15:40
 *
 */

namespace App\Console\Commands\ThirdOrder;

use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\SBTECHAPIService;
use App\Services\UserService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;


class SBTECHOrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'sbtech';
    protected $description = 'SBTECH体育注单拉取';
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;  //已结算
    public $latestUpdateKey_Unrecon;  //未结算

    public $sbtechService;
    public $redis;
    public $lockRedisKeyTime = RedisKeys::SBTECH_LOCK_AUTOFIX_TIME;
    public $updateRedisKeyReckonTime = RedisKeys::SBTECH_UPDATE_RECKON_TIME;
    public $updateRedisKeyUnreckonTime= RedisKeys::SBTECH_UPDATE_UNRECKON_TIME;

    const ORDER_STATUS_WAITING = 0;//等待
    const ORDER_STATUS_ACCEPT = 1;//接受
    const ORDER_STATUS_SETTLED = 2;//结算
    const ORDER_STATUS_CANCELED = 3;//取消
    const ORDER_STATUS_REFUSED = 4;//拒绝

    // Status: 注单状态:0=等待；1=接受；2=结算；3=取消；4=拒绝
    public static $TicketStatusEnum = [
        "Won" => "赢",
        "Lost" => "输",
        "Canceled," => "退款",
        "Draw" => "打和",
        "Open" => "等待",
        "Opened" => "进行中",
    ];

    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        // 5.第三方 partner id，名字，对应第三方表id
        $this->partnerName = "SBTECH";
        $this->sbtechService = new SBTECHAPIService();
        $this->redis = Redis::connection();
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_SPORT;
        $this->reportOrderType = self::GAME_TYPE_SPORT;
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        $startTime_reckon = OrderStrategy::getUpdateKey($this->lockRedisKeyTime, $this->updateRedisKeyReckonTime);  //获取已结算的起始时间
        $startTime_unreckon = OrderStrategy::getUpdateKey($this->lockRedisKeyTime, $this->updateRedisKeyUnreckonTime);//获取未结算的起始时间

        //$endTime_reckon = $endTime_unreckon = date('Y-m-d H:i:s');//获取未结算的结束时间
        $this->latestUpdateKey = $endTime_reckon = date('Y-m-d H:i:s',min(time(), strtotime($startTime_reckon) + $this->timeInterval - 3));
        $this->latestUpdateKey_Unrecon = $endTime_unreckon = date('Y-m-d H:i:s',min(time(), strtotime($startTime_unreckon) + $this->timeInterval - 3));

        $data_reckon = $this->sbtechService->bettingHistory($startTime_reckon,$endTime_reckon);  //已结算注单数据
        $data_unreckon = $this->sbtechService->openBets($startTime_unreckon,$endTime_unreckon); //未结算注单数据
        $reckon = $unreckon = [];
        if ($data_reckon !== false) {
            //$this->redis->set($this->updateRedisKeyReckonTime,date('Y-m-d H:i:s',strtotime($data_reckon['LastUpdateDate'])));
            $reckon = $data_reckon??[];
        }

        if ($data_unreckon !== false) {
            //$this->redis->set($this->updateRedisKeyUnreckonTime,date('Y-m-d H:i:s',strtotime($data_reckon['LastUpdateDate'])));
            $unreckon = $data_unreckon??[];
        }

        $data = array_merge($reckon,$unreckon);
        return $data;
    }

    /**
     * 处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     */
    public function load($data)
    {
        $this->updateTime = $data['update_time'];
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money']??0;
        $this->bonusMoney = $data['bonus_money']??0;
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];

    }
    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {
    }
    /*
     * 更新redis拉取注单时间
     */
    public function updateFlag($updateKey){
        // 更新 version_key
        $this->redis->set($this->updateRedisKeyReckonTime,$this->latestUpdateKey);
        $this->redis->set($this->updateRedisKeyUnreckonTime,$this->latestUpdateKey_Unrecon);
    }
    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($start_date)
    {

    }
}